# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: execute.py
# Time: 17:05


from functools import wraps

from bin.common.operation import ElementOperation
from crm.set import UserPags as P


class UserOperation(ElementOperation):
    def __init__(self, driver, log):
        super(UserOperation, self).__init__(driver, log)

    def transition(self):
        self.smart_click_element(P.过渡页.allow_but)
        self.smart_click_element(P.过渡页.jurisdiction_noe_but)
        self.smart_click_element(P.过渡页.jurisdiction_two_but)
        setting = self.is_element_exist(P.过渡页.set_but)
        if setting:
            self.click_element(P.过渡页.set_but)
            self.send_key_event("KEYCODE_BACK")
        self.click_element(P.过渡页.my_but)

    def login(self, useranme, password):
        if self.is_element_exist(P.登录页.login_registr_but):
            self.click_element(P.登录页.login_registr_but)
            self.click_element(P.登录页.switch_account_but)
            self.input_value(P.登录页.account_input, useranme, clear_first=True)
            self.input_value(P.登录页.pwd_input, password, clear_first=True)
            self.check_chckbox(P.登录页.agree_agreement_but)
            self.click_element(P.登录页.login_but)

    def logout(self):
        self.click_element(P.退出页.setting_but)
        self.click_element(P.退出页.logout_but)
        self.click_element(P.退出页.notarize_but)


class LoginOut(object):
    def __init__(self, select):
        self.select = select

    def __call__(self, func):
        @wraps(func)
        def select_login_logout(*args, **kwargs):
            if self.select == "login":
                self.login()
                return func()
            elif self.select == "logout":
                self.logout()
                return func(*args, **kwargs)
            else:
                raise ("选择登陆或退出有误！")

        return select_login_logout

    def login(self):
        pass

    def logout(self):
        pass
